# -*- coding: utf-8 -*-
'''
@Software: PyCharm
@File    : barrage_text_match.py
@Author  : Bryan SHEN
@E-mail  : m18801919240_3@163.com
@Site    : Shanghai, China
@Time    : 2021-09-02
@Description: 
'''

import json
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_BASE = BASE_DIR + "/libs/"

from LiveBarrageSentiment.utils.RESPONSE import RET


class BarrageTextMatch(object):

    def __init__(self):

        self.load_data()

    def load_data(self):

        self.L1_list = ["直播效果", "促销优惠", "互动问答", "价格", "正品", "下单", "物流", "包装", "产品相关", "功效", "通用评论"]

        self.base = json.loads(open(DATA_BASE + "structure.json", encoding="utf-8").read())
        self.product_keywords = [ex.strip() for ex in open(BASE_DIR + "/libs/products.txt", encoding="utf-8").readlines()]

    def l1_keyword_match(self, text):

        for L1 in self.L1_list:

            entity_words = self.base[L1]["words"]

            if L1 == "产品相关":
                entity_words = self.product_keywords + self.base["产品相关"]["words"]
                # text等于产品名的话, 一般属于互动问答
                if text in entity_words:
                    return 0, text, "互动问答"

            if L1 in ["功效", "下单", "促销优惠", "互动问答", "正品"]:
                if text in entity_words:
                    return 1, text, L1

            # 中性
            for neu_kw in self.base[L1]["neu2"]:
                if neu_kw in text:
                    return 0, neu_kw, L1
            for w in entity_words:
                for neu1_kw in self.base[L1]["neu1"]:
                    if w in text and neu1_kw in text:
                        return 0, w+"&"+neu1_kw, L1
            # 负面
            for neg2_kw in self.base[L1]["neg2"]:
                if neg2_kw in text:
                    return -1, neg2_kw, L1
            for w in entity_words:
                for neg1_kw in self.base[L1]["neg1"]:
                    if w in text and neg1_kw in text:
                        return -1, w+"&"+neg1_kw, L1
            # 正面
            for pos2_kw in self.base[L1]["pos2"]:
                if pos2_kw in text:
                    return 1, pos2_kw, L1
            for w in entity_words:
                for pos1_kw in self.base[L1]["pos1"]:
                    if w in text and pos1_kw in text:
                        return 1, w+"&"+pos1_kw, L1

        return 0, "", ""

    def run(self, texts):

        # 构建 api接口返回数据
        return_dict = {'status': RET.OK, 'response': 'success', 'feedback': {}}

        feedback = []
        texts = [str(text).lower() for text in texts]
        for i, text in enumerate(texts):
            emotion, point, L1 = self.l1_keyword_match(text)
            feedback.append({
                "text": text,
                "emotion": emotion,
                "subject": L1,
                "point": point
            })

        return_dict["feedback"] = feedback

        return json.dumps(return_dict, ensure_ascii=False)


if __name__ == '__main__':

    texts = ["这声音也太小了", "什么时候发货", "已拍", "3只多少钱。"]

    o = BarrageTextMatch()

    result = o.run(texts)

    print(result)



